package com.cy.controller;

import java.util.List;
import java.util.Map;

import com.cy.annotation.SysLog;
import com.cy.utils.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import javax.servlet.http.HttpServletRequest;

import com.cy.entity.PayWay;
import com.cy.service.PayWayService;
import org.springframework.web.multipart.MultipartFile;


/**
 * 
 * 
 * @author chenyi
 * @email qq228112142@qq.com
 * @date 2017-08-10 08:56:33
 */
@Controller
@RequestMapping("payway")
public class PayWayController {
	@Autowired
	private PayWayService payWayService;
	
	/**
	 * 列表
	 */
    @ResponseBody
	@RequestMapping("/list")
	@RequiresPermissions("payway:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<PayWay> payWayList = payWayService.queryList(query);
		int total = payWayService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(payWayList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}

	/**
	 * 上传文件
	 */
	@ResponseBody
	@RequestMapping("/upload")
	public R upload(@RequestParam("file") MultipartFile file) throws Exception {
		if (file.isEmpty()) {
			throw new RRException("上传文件不能为空");
		}
		//上传文件
		String url = UploadUtil.uploadImage(file.getOriginalFilename(),file.getInputStream());
		//保存文件信息
		PayWay payway=new PayWay();

		return R.ok().put("url", url);
	}

    /**
     * 跳转到新增页面
     **/
    @RequestMapping("/add")
    @RequiresPermissions("payway:save")
    public String add(){
        return "payway/add.jsp";
    }

    /**
     *   跳转到修改页面
     **/
    @RequestMapping("/edit/{id}")
    @RequiresPermissions("payway:update")
    public String edit(HttpServletRequest request, Model model, @PathVariable("id") Long id){
			PayWay payWay = payWayService.queryObject(id);
        model.addAttribute("model", payWay);
        return "payway/edit.jsp";
    }

	/**
	 * 信息
	 */
    @ResponseBody
	@RequestMapping("/info/{id}")
	@RequiresPermissions("payway:info")
	public R info(@PathVariable("id") Long id){
		PayWay payWay = payWayService.queryObject(id);
		
		return R.ok().put("payWay", payWay);
	}
	
	/**
	 * 保存
	 */
    @ResponseBody
	@SysLog("保存支付方式")
	@RequestMapping("/save")
	@RequiresPermissions("payway:save")
	public R save(@RequestBody PayWay payWay){
		List<PayWay> payWayList=payWayService.findByCode(payWay.getCode());
		if(payWayList!=null&&payWayList.size()>0){
			throw new RRException("支付方式编码已存在!");
		}
		payWayService.save(payWay);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@SysLog("修改支付方式")
    @ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("payway:update")
	public R update(@RequestBody PayWay payWay){
		PayWay oldPayWay=payWayService.queryObject(payWay.getId());
		if(!oldPayWay.getCode().equals(payWay.getCode())){
			List<PayWay> payWayList=payWayService.findByCode(payWay.getCode());
			if(payWayList!=null&&payWayList.size()>0){
				throw new RRException("支付方式编码已存在!");
			}
		}

		payWayService.update(payWay);

		return R.ok();
	}
	
	/**
	 * 删除
	 */
    @ResponseBody
	@SysLog("删除支付方式")
	@RequestMapping("/delete")
	@RequiresPermissions("payway:delete")
	public R delete(@RequestBody Long[] ids){
		payWayService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
